#
# Extract field names from an input shapefile
#

import sys, os

# global options
ONLY_FIELDS = "N"
USING_CSV = "N"

# process runtime input

if len(sys.argv) < 2:
  print "\n" + sys.argv[0] + " <shapefile path> <options> [field name] [field name]"
  print "-------------------------------------------------------------------------------------"
  print " Options:\t --csv <filename>\t Exports fields to csv file instead of stdout."
  print "         \t --onlyFields\t\t Just extract field names, not corresponding values."
  sys.exit()

inputShape = sys.argv[1]

for n in range(len(sys.argv)):
  if sys.argv[n] == "--csv":
    USING_CSV = "Y"
  elif sys.argv[n] == "--onlyFields":
    ONLY_FIELDS = "Y"

try:
  import arcpy
except ImportError:
  print "\nfailed to load arcpy module.  Are you using a workstation with ArcGIS 10 installed?\n"
  sys.exit()
  
try: 
  fields = arcpy.ListFields(inputShape, "", "All")
except Exception as e:
  print e.message
  sys.quit()
  
if ONLY_FIELDS == "Y":
  for field in fields:
    if field.type != "Geometry":
      print field.name + "(" + field.type + ") -- ",
  sys.exit()    

